<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\QuickEntrance;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class
QuickEntranceController extends AdminController
{
    /**
     * Author:Zhongyuan Lu
     * 前台快捷入口列表展示
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new QuickEntrance(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');     //入口名称
            $grid->column('desc');      //简介
            $grid->column('href');      //路径

            $grid->column('image')->image(config('setting.uploadUrl'),100,100);

            //归属 0 普通人  1 经纪人
            $grid->column('ascription')->display(function ($ascription) {
                $options = config('setting.ascription_options');
                return $options[$ascription];
            });

            //页面类型 0: h5     1: 小程序     2: app
            $grid->column('page_type')->display(function ($page_type) {
                $options = config('setting.page_type');
                return $options[$page_type];
            });

            //状态  0 关闭  1开启
            $grid->column('status')->display(function ($status) {
                $options = config('setting.status_options');
                return $options[$status];
            });

            $grid->column('sort');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Author:Zhongyuan Lu
     * 首页快捷入口详情
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new QuickEntrance(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('desc');
            $show->field('href');
            $show->field('image')->image();
            $show->field('ascription')->using([0 => '普通用户', 1=> '经纪人']);
            $show->field('page_type')->using([0 => 'h5', 1=> '小程序', 2=>'app']);
            $show->field('status')->using([0=>'关闭',1=>'开启']);
            $show->field('sort');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Author:Zhongyuan Lu
     * 首页快捷入口添加
     * @return Form
     */
    protected function form()
    {
        return Form::make(new QuickEntrance(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('desc');
            $form->text('href')->placeholder('/pages/home/home');
            $form->image('image')->saveFullUrl();
            $form->radio('ascription')->options([0 => '普通用户', 1=> '经纪人'])->default(0);
            $form->radio('page_type')->options([0 => 'h5', 1=> '小程序', 2=>'app'])->default(1);
            $form->radio('status')->options([0=>'关闭',1=>'开启'])->default(1);
            $form->number('sort');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
